﻿/*!	@file	r_scenegraph_matrix_hierarchy.cpp
	@brief	シーングラフ行列階層
Copyright (c) 2010 Yuya Yamaguchi

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include "r_scenegraph_matrix_hierarchy.h"


cRSceneGraphMatrixHerarchyIterator :: cRSceneGraphMatrixHerarchyIterator ( const cRSceneGraphMatrixHerarchyIterator& src )
	:
	hierarchy ( src.hierarchy ),
	myIndex ( src.myIndex )
{
}

cRSceneGraphMatrixHerarchyIterator :: cRSceneGraphMatrixHerarchyIterator ( cRSceneGraphMatrixHierarchy* _hierarchy )
	:
	hierarchy ( _hierarchy ),
	myIndex ( eRSceneGraphMatrixHierarchy_RootIndex )
{
}

cRSceneGraphMatrixHerarchyIterator :: ~cRSceneGraphMatrixHerarchyIterator()
{
}

r_bool	cRSceneGraphMatrixHerarchyIterator :: getHasChild() const
{
	return	hierarchy->child[myIndex] != eRSceneGraphMatrixHierarchy_BadIndex;
}

r_bool	cRSceneGraphMatrixHerarchyIterator :: getHasSibling() const
{
	return	hierarchy->sibling[myIndex] != eRSceneGraphMatrixHierarchy_BadIndex;
}

r_ui16	cRSceneGraphMatrixHerarchyIterator :: getChildIndex() const
{
	return	hierarchy->child[myIndex];
}

r_ui16	cRSceneGraphMatrixHerarchyIterator :: getSiblingIndex() const
{
	return	hierarchy->sibling[myIndex];
}

void	cRSceneGraphMatrixHerarchyIterator :: setChildIndex ( r_si32 index )
{
	hierarchy->child[myIndex]	= index;
}

void	cRSceneGraphMatrixHerarchyIterator :: setSiblingIndex ( r_si32 index )
{
	hierarchy->sibling[myIndex]	= index;
}

void	cRSceneGraphMatrixHerarchyIterator :: addChild()
{
	if ( getHasChild() ) {
		r_si32	childindex	= hierarchy->child[myIndex];
		while ( hierarchy->sibling[childindex] != eRSceneGraphMatrixHierarchy_BadIndex ) {
			childindex		= hierarchy->sibling[childindex];
		}
		hierarchy->child.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
		hierarchy->sibling.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
		hierarchy->sibling[childindex]	= hierarchy->sibling.size();
	} else {
		hierarchy->child.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
		hierarchy->sibling.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
		hierarchy->child[myIndex]		= hierarchy->child.size();
	}
}

void	cRSceneGraphMatrixHerarchyIterator :: addSibling()
{
	r_si32	siblingindex	= hierarchy->sibling[myIndex];
	while ( hierarchy->sibling[siblingindex] != eRSceneGraphMatrixHierarchy_BadIndex ) {
		siblingindex		= hierarchy->sibling[siblingindex];
	}
	hierarchy->child.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
	hierarchy->sibling.push_back ( eRSceneGraphMatrixHierarchy_BadIndex );
	hierarchy->sibling[siblingindex]	= hierarchy->sibling.size();
}

r_bool	cRSceneGraphMatrixHerarchyIterator :: moveChild()
{
	if ( getHasChild() ) {
		myIndex		=	hierarchy->child[myIndex];
		return true;
	}
	return	false;
}

r_bool	cRSceneGraphMatrixHerarchyIterator :: moveNext()
{
	if ( getHasSibling() ) {
		myIndex		=	hierarchy->sibling[myIndex];
		return true;
	}
	return	false;
}


const	r::math::Matrix34&	cRSceneGraphMatrixHerarchyIterator :: getLocalMatrix() const
{
	return	hierarchy->localMatrix[myIndex];
}
void	cRSceneGraphMatrixHerarchyIterator :: setLocalMatrix ( const r::math::Matrix34& mat )
{
	hierarchy->localMatrix[myIndex]		= mat;
}
const	r::math::Matrix34&	cRSceneGraphMatrixHerarchyIterator :: getGlobalMatrix() const
{
	return	hierarchy->globalMatrix[myIndex];
}
void	cRSceneGraphMatrixHerarchyIterator :: setGlobalMatrix ( const r::math::Matrix34& mat )
{
	hierarchy->globalMatrix[myIndex]	= mat;
}

// --------------------------------------------------------


// >>>>>>>>>> RTTI >>>>>>>>>>
#include "../rtti/r_rtti_reflection_implement.h"
RRTTI_REFLECTION_IMPLEMENT ( cRSceneGraphMatrixHierarchy, r::rtti::ClassBase,
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( child ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( sibling ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( localMatrix ),
							 RRTTI_REFLECTION_IMPLEMENT_TYPE ( globalMatrix ),
						   )
// <<<<<<<<<< RTTI <<<<<<<<<<

cRSceneGraphMatrixHierarchy :: cRSceneGraphMatrixHierarchy()
{
}

cRSceneGraphMatrixHierarchy :: ~cRSceneGraphMatrixHierarchy()
{
}

void	cRSceneGraphMatrixHierarchy :: calculate ( const r::math::Matrix34& mat )
{
	calcHierarchy ( mat, 0 );
}

r_si32	cRSceneGraphMatrixHierarchy :: getMatrixNum() const
{
	return	localMatrix.size();
}

void	cRSceneGraphMatrixHierarchy :: setMatrixNum ( r_si32 num )
{
	r::math::Matrix34	mat;
	mat.identity();
	child.resize ( eRSceneGraphMatrixHierarchy_BadIndex );
	sibling.resize ( eRSceneGraphMatrixHierarchy_BadIndex );
	localMatrix.resize ( num, mat );
	globalMatrix.resize ( num, mat );
}

const r::math::Matrix34&	cRSceneGraphMatrixHierarchy :: getLocalMatrix ( r_si32 index ) const
{
	return	localMatrix[index];
}

const r::math::Matrix34&	cRSceneGraphMatrixHierarchy :: getGlobalMatrix ( r_si32 index ) const
{
	return	globalMatrix[index];
}

r::math::Matrix34&	cRSceneGraphMatrixHierarchy :: refLocalMatrix ( r_si32 index )
{
	return	localMatrix[index];
}

cRSceneGraphMatrixHerarchyIterator	cRSceneGraphMatrixHierarchy :: getRootHierarchy()
{
	cRSceneGraphMatrixHerarchyIterator i ( this );
	return	i;
}

void	cRSceneGraphMatrixHierarchy :: calcHierarchy ( const r::math::Matrix34& mat, r_si32 index )
{
	// 兄弟行列を優先したらキャッシュ効きやすい?
	if ( sibling[index] != eRSceneGraphMatrixHierarchy_BadIndex ) {
		calcHierarchy ( mat, sibling[index] );
	}
	if ( child[index] != eRSceneGraphMatrixHierarchy_BadIndex ) {
		calcHierarchy ( mat * localMatrix[index], child[index] );
	}
}

